section .data
	errno dq 0
	addr dq 0

MAP_FAILED equ -1
MAP_ADDR_BASE equ 0x0
;MAP_ADDR_BASE equ 0x102E0400
MAP_LEN equ 40960
;MAP_LEN equ 0xffffffffffffffff
PROT_READ_WRITE equ 3
MAP_SHARED_ANON equ 0x1001
;MAP_SHARED_ANON equ 0x1111111

%define mk64 0x2000000+

section .text
;extern errno
;extern __errno_location
extern _strerror
extern _printf
;if use ld
;global _start
;if use gcc
global _main
;_start:
_main:
	and rsp,~0xffff			;堆栈对齐 equ 0xffffffffffff0000
	
	mov rax,mk64 197				;mmap NO
	mov rdi,MAP_ADDR_BASE				;map address
	mov rsi,MAP_LEN				;map size
	mov rdx,PROT_READ_WRITE
	mov r10,MAP_SHARED_ANON
	mov r8,-1				;忽略fd
	mov r9,0				;offset
	syscall
	;cmp rax,0xfffffffffffff001
	cmp rax,0xfff
	ja next
	push rax
	;neg rax
	mov [rel errno],rax
	pop rax
	or rax,-1
next:
	cmp rax,MAP_FAILED
	;cmp rax,0
	;js map_failed
	je map_failed
	
	;just test
	;++++++++++++++++++++++++++++
	;mov rdi,[rel errno]

	;++++++++++++++++++++++++++++


	mov [rel addr],rax
	;lea rbx,[rel addr]
	;mov rbx,addr
	mov rsi,[rel addr]
	;mov rdi,msg_success
	lea rdi,[rel msg_success]
	call _printf
	
	mov rax,mk64 73
	;mov rbx,addr
	;lea rbx,[rel addr]
	mov rdi,[rel addr]
	mov rsi,MAP_LEN
	syscall
	jmp exit
	
map_failed:	
	;mov rdi,0xb
	;call __errno_location
	;mov rax,qword [rax]
	;mov rsi,[errno]
	
	;mov rbx,errno
	;lea rbx,[rel errno]
	mov rdi,[rel errno]
	call _strerror
	mov rsi,rax
	;mov rdi,msg_failed
	lea rdi,[rel msg_failed]
	call _printf

exit:
	mov rax,mk64 1		;exit NO
	mov rdi,0		;error_code
	syscall
	
	msg_success: db "map successed , addr at %p",0ax,0
	msg_failed: db "map failed ,due to %s",0ax,0
	